//
//  HomeWeiboFrameModel.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/25.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "HomeWeiboFrameModel.h"
#import "HomeWeiboModel.h"
#import "WXLabel.h"
#import "UIImage+MultiFormat.h"
#import "UserModel.h"

@implementation HomeWeiboFrameModel


- (void)setWeiboModel:(HomeWeiboModel *)weiboModel{
    
    if (_weiboModel != weiboModel) {
        
        _weiboModel = weiboModel;
        //重新计算
        [self layoutFrame];
        
    }
    
    
}

- (void)layoutFrame{
    
    //根据 weiboModel计算
        
    /**
     * 微薄视图的frame
     */
    self.frame = CGRectMake(0, 74, kScreenWidth - 20, 0);      //作为weiboView的外观尺寸(高度还未确定)
    
    /**
     * 微博文字的frame
     */
//    1、限定宽度
    CGFloat textWidth = CGRectGetWidth(self.frame);
    
//    2、计算内容高度
    NSString *text = self.weiboModel.text;
    CGFloat textHeight = [WXLabel getTextHeight:15 width:textWidth text:text linespace:5.0];
    
    self.textFrame = CGRectMake(10, 0, textWidth, textHeight + 5);      //统一加间距，线面无须再加
    
    /**
     * 源微博的内容frame
     */
    if (self.weiboModel.srWeiboModel != nil) {        //是转发的微薄
        
        /**
         * 加上源微博用户昵称
         */
        NSString *srText = self.weiboModel.srWeiboModel.text;
        NSString *srUserName = self.weiboModel.srWeiboModel.userModel.screen_name;
        NSString *newSrWeiboText = [NSString stringWithFormat:@"@%@：%@",srUserName,srText];
        
        CGFloat srTextWidth = textWidth - 20;       //内部缩进
        CGFloat srTextHeight = [WXLabel getTextHeight:14 width:srTextWidth text:newSrWeiboText linespace:5.0];
        
        CGFloat srTextY = CGRectGetMaxY(self.textFrame) + 10;       //10 为内边距
        self.srTextFrame = CGRectMake(20, srTextY, srTextWidth, srTextHeight);
    
    /**
     * 源微博图片
     */
    NSString *thumbnailImage = self.weiboModel.srWeiboModel.thumbnailImage;     //utlStr
    if (thumbnailImage != nil) {
        CGFloat X = CGRectGetMinX(self.srTextFrame);
        CGFloat Y = CGRectGetMaxY(self.srTextFrame);

//          获取图片的尺寸
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:thumbnailImage]];
        UIImage *image = [UIImage sd_imageWithData:imageData];
        if (image.size.width > srTextWidth) {
            self.imgFrame = CGRectMake(X, Y, srTextWidth, image.size.height);   //图片尺寸
        }else {
            self.imgFrame = CGRectMake(X, Y, image.size.width, image.size.height);   //图片尺寸
        }
    }
    
    /**
     * 源微薄的背景
     */
    CGFloat bgX = CGRectGetMinX(self.textFrame);    //与微薄文字对齐
    CGFloat bgY = CGRectGetMaxY(self.textFrame);    //在微薄文字的下面
    CGFloat bgW = CGRectGetWidth(self.textFrame);
    CGFloat bgH = CGRectGetMaxY(self.srTextFrame);
    
    if (thumbnailImage != nil) {        //原微博有图片
        bgH = CGRectGetMaxY(self.imgFrame);
    }
    
    bgH -= CGRectGetMaxY(self.textFrame);
    bgH += 10;
    self.bgImgFrame = CGRectMake(bgX, bgY, bgW, bgH);
    
    }else {     //是原创微博
        
        NSString *thumbnailImage = self.weiboModel.thumbnailImage;
        if (thumbnailImage != nil) {
            CGFloat imgX = CGRectGetMinX(self.textFrame);
            CGFloat imgY = CGRectGetMaxY(self.textFrame);
            
//          获取图片的尺寸（保持图片不变形）
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:thumbnailImage]];
            UIImage *image = [UIImage sd_imageWithData:imageData];
            if (image.size.width > CGRectGetWidth(self.textFrame)) {
                self.imgFrame = CGRectMake(imgX, imgY, CGRectGetWidth(self.textFrame), image.size.height);   //图片尺寸
            }else {
                self.imgFrame = CGRectMake(imgX, imgY, image.size.width, image.size.height);   //图片尺寸
            }
        }
        
    }
    
    /**
     * 计算微博视图的高度：微博视图最底部子视图的Y坐标
     */
    CGRect frame = self.frame;
    if (self.weiboModel.srWeiboModel != nil) {      //转发
        frame.size.height = CGRectGetMaxY(_bgImgFrame) + 5;     //包含转发微薄的整体frame
    }
    else if(self.weiboModel.thumbnailImage != nil) {        //有图片
        frame.size.height = CGRectGetMaxY(_imgFrame) + 10;
    }
    else {      //原创没图
        frame.size.height = CGRectGetMaxY(_textFrame) + 5;
    }
    self.frame = frame;
    
    
}

@end
